package com.bnlr.lrparking.appui.stall;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.appui.login.VerificaActivity;
import com.bumptech.glide.Glide;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.BaseArrayBean;
import com.bnlr.lrparking.model.MyPublishBean;
import com.bnlr.lrparking.model.StallDetailBean;
import com.bnlr.lrparking.appui.publish.my.SelectMyPublishActivity;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * @author hp
 * @date 2017/10/18
 * 求租求购 详情
 */

public class StallBegRentBuy extends BaseActivity {
    private Unbinder unbinder;
    private  MyPublishBean.DataBean detailBean;
    @BindView(R.id.deal_detail_title)
    TextView title;
    @BindView(R.id.deal_detail_date)
    TextView date;
    @BindView(R.id.publish_detail_category)
    TextView category;
    @BindView(R.id.deal_detail_price)
    TextView price;
    @BindView(R.id.deal_detail_address)
    TextView address;
    @BindView(R.id.deal_detail_spaceType)
    TextView spaceType;
    @BindView(R.id.imageView57)
    ImageView ivAuth;
    @BindView(R.id.tv_auth_status)
    TextView tvAuthStatus;
    @BindView(R.id.desc)
    TextView desc;
    @BindView(R.id.imageView55)
    ImageView ivHead;
    @BindView(R.id.image_datum)
    ImageView image_datum;
    @BindView(R.id.deal_detail_name)
    TextView name;
    @BindView(R.id.checkBox_college)
    CheckedTextView checkCollect;
    @BindView(R.id.textView23)
    TextView registerDate;
    @BindView(R.id.textView24)
    TextView messageCount;
    @BindView(R.id.stall_detail_status_int)
    TextView tvStatusInt;
    @BindView(R.id.rely_havastall)
    RelativeLayout rely_havastall;//我有车位
    @BindView(R.id.beg_stall_detail_bottom)
    RelativeLayout bottom;
    private int IsFavorite;
    private int stallId;
    private StallDetailBean.DataBean db = null;
    private boolean isWaiting = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stall_begrentbuy);
        unbinder = ButterKnife.bind(this);
        Intent intent = getIntent();
        stallId = intent.getIntExtra("stall_id",0);
        isWaiting = intent.getBooleanExtra("is_waiting",false);
        if (isWaiting) {
            //是否是从我的发布 正在审核中点进来的
            bottom.setVisibility(View.GONE);
        }
        getStallBegRentBuyDetail(stallId);

    }

    /**
     * 获取求租求购详情
     * @param stallId
     */
    private void getStallBegRentBuyDetail(int stallId) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP_SHOW, LrConstant.POST);
            mRequest.add("action", "GetParkSpaceDetail");
            mRequest.add("parkSpaceId",stallId);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            // 添加到请求队列
            CallServer.getRequestInstance().add(StallBegRentBuy.this, mRequest,
                    new CustomHttpListener<StallDetailBean>(StallBegRentBuy.this, true, StallDetailBean.class) {
                        @Override
                        public void doWork(StallDetailBean data, String code) {
                            if (code.equals("1")) {
                                if (data.getData() != null && data.getData().get(0) != null) {
                                    db = data.getData().get(0);
                                    title.setText(db.getTitle());
                                    if (db.getSpaceOwerType() == 1) {
                                        category.setText("开发商");
                                    }
                                    date.setText("发布于" + CommonUtil.timeGap(db.getCreateTime()));
                                    price.setText(db.getPrice());
                                    address.setText("地址：" + db.getArea());
                                    spaceType.setText("车位类型：" + db.getSpaceTypeString());
                                    if (db.getUserModel() != null) {
                                        StallDetailBean.DataBean.UserModelData userModelData = db.getUserModel();
                                       if ("1".equals(userModelData.getIsRealNameAuth())) {
                                           ivAuth.setImageResource(R.mipmap.details_user_true);
                                           tvAuthStatus.setText("该用户已通过实名认证");
                                       }
                                        name.setText(userModelData.getName()+"");
                                        Glide.with(StallBegRentBuy.this)
                                                .load(userModelData.getPhoto())
                                                .bitmapTransform(new CropCircleTransformation(StallBegRentBuy.this))
                                                .error(R.mipmap.personal_head)
                                                .into(ivHead);
                                        String date = userModelData.getCreateTime().split(" ")[0];
                                        registerDate.setText(date.split("-")[0] +"年"+date.split("-")[1] +"月"+date.split("-")[2] +"日注册");
                                        messageCount.setText("近一个月在利人停车发布过"+userModelData.getPublishCount()+"条信息");
                                    } else {
                                        image_datum.setVisibility(View.GONE);
                                    }
                                    if (!TextUtils.isEmpty(db.getDescription())) {
                                        desc.setText(db.getDescription());
                                    }
                                    IsFavorite = db.getIsFavorite();
                                    if (IsFavorite == 0){
                                        checkCollect.setChecked(false);

                                    }else {
                                        checkCollect.setChecked(true);
                                    }

                                }

                            } else {
                                ToastUtils.showShort(data.getMsg());
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            if (!code.equals("1")) {
                                image_datum.setVisibility(View.GONE);
                            }
                        }
                    }, true);
        } catch (Exception e) {
            Log.e("StallBegRentBuy",e.getMessage()+"");
        }
    }

    @OnClick({R.id.image_datum,R.id.iv_back,R.id.rely_stalldeal_phone,R.id.rely_stalldeal_coll,R.id.rely_havastall})
    public void StallBegOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.image_datum:
                //资料
                if (db != null) {
                    startActivity(new Intent(this, PersonageMaterialActivity.class)
                            .putExtra("auth_bean", db.getUserModel())
                            .putExtra("user_id", db.getUserId()));
                }
                break;
            case R.id.rely_stalldeal_phone:
                if (!AppConfig.getInstance().getBoolean("isLogin",false)) {
                    startActivity(new Intent(this, VerificaActivity.class));
                    return;
                }
                if (db != null) {
                    if (AppConfig.getInstance().getString("uid", "").equals(db.getUserId()+"")) {
                        ToastUtils.showShort("别闹，您怎么给自己打电话呢？");
                        return;
                    }
                }
                CarCommon carCommon = new CarCommon();
                carCommon.phoneDialog(this);
                break;
            case R.id.rely_stalldeal_coll:
                if (!AppConfig.getInstance().getBoolean("isLogin",false)) {
                    startActivity(new Intent(this, VerificaActivity.class));
                    return;
                }
                if (IsFavorite == 0){
                    setCollege(stallId,"FavoriteSpace");
                }else {
                    setCollege(stallId,"CancelFavoriteSpace");
                }
                break;
            case R.id.rely_havastall:
                if (!AppConfig.getInstance().getBoolean("isLogin",false)) {
                    startActivity(new Intent(this, VerificaActivity.class));
                    return;
                }
                if (AppConfig.getInstance().getString("uid", "").equals(db.getUserId()+"")) {
                    ToastUtils.showShort("这是您自己发布的信息哦~");
                    return;
                }
                if (db != null) {
                    startActivity(new Intent(this, SelectMyPublishActivity.class)
                            .putExtra("select_publish_type", db.getTypeInt())
                            .putExtra("submit_stall_id", db.getId()));
                }
                break;
            default:
                break;
        }
    }
    //收藏的接口
    private void setCollege(int id, String FavoriteSpace){
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            /**
             * IsFavorite: 0是未收藏  1是已收藏
             * action: FavoriteSpace是从藏接口  CancelFavoriteSpace是取消收藏接口
             */
            mRequest.add("action", FavoriteSpace);
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("id", id);

            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<BaseArrayBean>(baseContext, true, BaseArrayBean.class) {
                        @Override
                        public void doWork(BaseArrayBean data, String code) {
                            if ("1".equals(code)) {
                                if (IsFavorite == 0){
                                    ToastUtils.showShort("收藏成功");
                                    checkCollect.setChecked(true);
                                    IsFavorite = 1;

                                }else {
                                    ToastUtils.showShort("已取消收藏");
                                    checkCollect.setChecked(false);
                                    IsFavorite = 0;

                                }
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
